home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
The World of Computer Software.iso
/
vsc92nov.zip
/
except.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-02
|
3KB
|
162 lines
/*
* except.c -- Implementation of Scheme exception handling
*
* (C) m.b (Matthias Blume), Wed May 13 19:03:03 MET DST 1992, HUB/Ger
* Humboldt-University of Berlin, Germany
*/
# ident "@(#)except.c (C) M.Blume, Humboldt-Uni Berlin, 1.2"
# include <stdio.h>
# include <stdarg.h>
# include <stdlib.h>
# include <string.h>
# include "Cont.h"
# include "String.h"
# include "except.h"
# include "m-except.h"
# include "io.h"
# include "storext.h"
# include "type.h"
# include "mode.h"
# define MESSAGE_BUF_SIZE 256
static char *message_buf = NULL;
static unsigned message_buf_len = 0;
static unsigned message_len = 0;
static void message_putc (int c, void *ignore)
{
char *tmp;
if (message_len >= message_buf_len) {
if (message_buf_len == 0)
tmp = malloc (MESSAGE_BUF_SIZE);
else
tmp = realloc (message_buf, message_buf_len + MESSAGE_BUF_SIZE);
if (tmp == NULL)
fatal ("Out of message buffer space");
message_buf = tmp;
message_buf_len += MESSAGE_BUF_SIZE;
}
message_buf [message_len++] = c;
}
static void message (
const char *prefix, const char *template, va_list ap, putc_proc pp, void *cd)
{
void *x;
char *s;
int i;
unsigned int u;
double d;
char buf [128];
putc_string (prefix, pp, cd);
while (template [0]) {
if (template [0] == '%') {
switch (template[1]) {
case '%':
(* pp) (template[1], cd);
break;
case 'w':
x = va_arg (ap, void *);
write_object (x, pp, cd);
break;
case 'd':
x = va_arg (ap, void *);
display_object (x, pp, cd);
break;
case 's':
s = va_arg (ap, char *);
putc_string (s, pp, cd);
break;
case 'i':
i = va_arg (ap, int);
sprintf (buf, "%d", i);
putc_string (buf, pp, cd);
break;
case 'u':
u = va_arg (ap, unsigned);
sprintf (buf, "%u", u);
putc_string (buf, pp, cd);
break;
case 'f':
d = va_arg (ap, double);
sprintf (buf, "%f", d);
putc_string (buf, pp, cd);
break;
default:
--template;
break;
}
++template;
} else
(* pp) (template [0], cd);
++template;
}
va_end (ap);
}
static ScmString *message_string (
const char *prefix, const char *template, va_list ap)
{
ScmString *string;
message_len = 0;
message (prefix, template, ap, message_putc, NULL);
string = getmem (ScmType (String), sizeof (ScmString) + message_len - 1);
string->length = message_len;
memcpy (string->array, message_buf, message_len);
return string;
}
void warning (const char *text, ...)
{
va_list ap;
va_start (ap, text);
message ("warning: ", text, ap, file_putc, stderr);
putc ('\n', stderr);
}
volatile void error (const char *text, ...)
{
void *eh, *tmp;
va_list ap;
va_start (ap, text);
tmp = message_string ("error: ", text, ap);
eh = ScmMode (SCM_ERROR_HANDLER_MODE);
ScmRaiseError (eh, tmp);
}
volatile void fatal (const char *text)
{
# ifndef EXIT_VOLATILE
extern volatile void exit (int);
# endif
fprintf (stderr, "fatal: %s\n", text);
exit (EXIT_FAILURE);
}
volatile void restriction (const char *text, ...)
{
void *eh, *tmp;
va_list ap;
va_start (ap, text);
tmp = message_string (
"violation of an implementation restriction: ", text, ap);
eh = ScmMode (SCM_ERROR_HANDLER_MODE);
ScmRaiseError (eh, tmp);
}
volatile void reset (const char *text)
{
fprintf (stderr, "System reset: %s\n", text);
ScmRaiseReset ();
}